

var app = angular.module('fruits',['ng','ngRoute']);

//创建一个parentCtrl
app.controller('parentCtrl', ['$scope','$location',
    function($scope,$location){
        $scope.jump = function(url){
            $location.path(url);
        }
}]);

//声明控制器mainCtrl main.html功能是支持搜索和分页加载
app.controller('mainCtrl',['$scope','$http',function($scope,$http){
    $scope.hasMore = true;
    //加载数据
    $http.get('data/dish_getbypage.php?start=0')
         .success(function(data){
            console.dir(data);
            $scope.dishList = data;
    });
    // 加载更多数据
    $scope.loadMore = function(){
        $http.get('data/dish_getbypage.php?start='+$scope.dishList.length)
             .success(function(data){
               //console.log($scope.dishList);
                if(data.length < 5){
                    $scope.hasMore = false;
                }
               $scope.dishList = $scope.dishList.concat(data);
            });
    };
    //添加搜索功能
    $scope.$watch('kw',function(){
        if($scope.kw){
            $http.get('data/dish_getbykw.php?kw='+$scope.kw)
                 .success(function(data){
                    $scope.dishList = data;
            })
        }
    });

}]);

//声明控制器 detailCtrl 根据传来的dishId查找搜索详情饼传过来
app.controller('detailCtrl',['$scope','$http','$routeParams',
    function ($scope,$http,$routeParams) {
        console.log($routeParams.dishId);
        $http.get('data/dish_getbyid.php?id='+$routeParams.dishId)
            .success(function (data) {
                console.log(data);
                $scope.dish = data[0];
            })
}]);

//声明控制器 orderCtrl 根据传来的dishId 把用户输入的所有信息和商品id传递给控制器
app.controller('orderCtrl', ['$scope','$http','$routeParams','$rootScope',
        function($scope,$http,$routeParams,$rootScope){
            //console.log($routeParams.dishId);
            $scope.order = {'did':$routeParams.dishId};
            $scope.submitOrder = function(){
                var args = $.param($scope.order);
                console.log(args);
                $http.get('data/order_add.php?'+args)
                     .success(function(data){
                        if(data.msg == 'succ'){

                            $rootScope.phone = $scope.order.phone;
                            $scope.succMsg ='下单成功，订单编号为:' + data.oid;
                            console.log( $rootScope.phone);
                        }else{
                            $scope.errMsg = '下单失败！'
                        }
                    })
        };


}]);

//声明控制器 myorderCtrl
app.controller('myOrderCtrl', ['$scope','$http','$rootScope',
    function($scope,$http,$rootScope){
        console.log($rootScope.phone);
    $http.get('data/order_getbyphone.php?phone='+$rootScope.phone)
         .success(function(data){
             $scope.orderList = data;
            console.log(data);
        })
}]);

//配置路由词典
app.config(function ($routeProvider){
    $routeProvider
        .when('/start',{
        templateUrl:'tpl/start.html'
        })
        .when('/main',{
            templateUrl:'tpl/main.html',
            controller:'mainCtrl'
        })
        .when('/detail',{
            templateUrl:'tpl/detail.html'
        })
        .when('/detail/:dishId',{
            templateUrl:'tpl/detail.html',
            controller:'detailCtrl'
        })
        .when('/order',{
            templateUrl:'tpl/order.html'
        })
        .when('/order/:dishId',{
            templateUrl:'tpl/order.html',
            controller:'orderCtrl'
        })
        .when('/myOrder',{
            templateUrl:'tpl/myOrder.html',
            controller:'myOrderCtrl'
        })
        .otherwise({redirectTo:'/start'});
});


